package com.huffman.smart_picture.common;

import com.huffman.smart_picture.domain.constants.CommonConstant;
import com.huffman.smart_picture.domain.entity.User;
import com.huffman.smart_picture.domain.enums.UserVipEnum;
import com.huffman.smart_picture.domain.vo.user.UserVo;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * @author DengLin
 * @title 用户会话信息
 * @date 2024/07/03
 */
@Data
public class UserSession implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;
    private String username;
    private String nickname;
    private String avatar;
    private String role;
    private String email;
    private Long expireTime;
    private Boolean isVip = false;
    private Date vipExpireTime;
    private Long vipNumber;
    private Map<Long, Integer> spaceUserRoleMap;

    public static UserSession convert(User user) {
        UserSession userSession = new UserSession();
        userSession.setId(user.getId());
        userSession.setUsername(user.getUsername());
        userSession.setNickname(user.getNickname());
        userSession.setAvatar(user.getAvatar());
        userSession.setRole(user.getRole());
        userSession.setEmail(user.getEmail());
        if (user.getVipExpireTime() != null && user.getVipExpireTime().after(new Date())) {
            userSession.setIsVip(UserVipEnum.VIP.getValue().equals(user.getIsVip()));
        }
        userSession.setVipNumber(user.getVipNumber());
        userSession.setVipExpireTime(user.getVipExpireTime());
        return userSession;
    }

    public static UserSession convert(UserVo userVo) {
        UserSession userSession = new UserSession();
        userSession.setId(userVo.getId());
        userSession.setUsername(userVo.getUsername());
        userSession.setNickname(userVo.getNickname());
        userSession.setAvatar(userVo.getAvatar());
        userSession.setRole(userVo.getRole());
        userSession.setEmail(userVo.getEmail());
        userSession.setExpireTime(userVo.getExpireTime());
        userSession.setIsVip(userVo.getIsVip());
        userSession.setVipNumber(userVo.getVipNumber());
        userSession.setVipExpireTime(userVo.getVipExpireTime());
        userSession.setSpaceUserRoleMap(userVo.getSpaceUserRoleMap());
        return userSession;
    }

    public boolean isAdmin(){
        return CommonConstant.ADMIN.equals(getRole());
    }
}
